/*
 * Copyright 2010 Yaroslav Klymko aka t3hnar
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ua.t3hnar.io.serializer.handlers;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.InputStream;

/**
 * @author Yaroslav Klymko aka t3hnar
 */
public abstract class FieldHandler<T> {
// ------------------------------ FIELDS ------------------------------

    private static final Log LOG = LogFactory.getLog(FieldHandler.class);

    private final String name;
    private final Class<T> instanceClass;

// --------------------------- CONSTRUCTORS ---------------------------

    protected FieldHandler(final String name, final Class<T> instanceClass) {
        if (name == null || instanceClass == null) {
            throw new IllegalArgumentException();
        }

        LOG.debug("Initializing FieldHandler; name: " + name + "; instanceClass: " + instanceClass);

        this.name = name;
        this.instanceClass = instanceClass;
    }

// --------------------- GETTER / SETTER METHODS ---------------------

    public Class<T> getInstanceClass() {
        LOG.debug("Getting instance class");
        return this.instanceClass;
    }

    public String getName() {
        LOG.debug("Getting name");
        return this.name;
    }

// ------------------------ CANONICAL METHODS ------------------------

    @Override
    public boolean equals(final Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        final FieldHandler that = (FieldHandler) o;
        return name.equals(that.getName());
    }

    @Override
    public int hashCode() {
        return name.hashCode();
    }

    @Override
    public String toString() {
        final StringBuilder builder = new StringBuilder();
        builder.append("FieldHandler");
        builder.append("{name='").append(this.name).append('\'');
        builder.append(", instanceClass=").append(this.instanceClass);
        builder.append('}');
        return builder.toString();
    }

// -------------------------- OTHER METHODS --------------------------

    public boolean isAssignable(final Class<?> type) {
        LOG.debug("Checking if is assignable; type: " + type);
        if (type == null) {
            throw new IllegalArgumentException("Type is null");
        }

        final boolean assignable = this.instanceClass.isAssignableFrom(type);
        LOG.debug("assignable: " + assignable);
        return assignable;
    }

    public abstract T toInstance(final InputStream inputStream) throws ToInstanceException;

    public abstract InputStream toStream(final Object instance) throws ToStreamException;
}
